/*
 * Copyright 2018-9-30 20:11:44 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.ao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.lap.scd.commons.ao.AbstractAO;
import com.lap.scd.commons.constant.RespCode;
import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.page.PageUtils;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.erp.ao.ContractAO;
import com.lap.scp.erp.core.ContractManager;
import com.lap.scp.erp.core.EmployeeManager;
import com.lap.scp.erp.domain.ContractDO;
import com.lap.scp.erp.query.ContractQueryVO;

/**
 * <pre>
 *   默认 合同信息应用实现类，提供合同信息相关的流程应用操作、查询等。
 *
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-9-30 20:11:44
 * @since 2.0.0
 */
@Service("contractAO")
public class DefaultContractAO extends AbstractAO implements ContractAO {
	/**
	 * 合同信息业务操作对象
	 */
	@Resource
	private ContractManager contractManager;

	@Resource
	private EmployeeManager employeeManager;

	@Override
	public Resp<ContractDO> insertContract(ContractDO contractDO, BindingResult errors) {
		Resp<ContractDO> resp = new Resp<>();
		try {
			super.validate(resp, errors, contractDO);
			if (resp.isSuccess()) {
				contractManager.insertContract(contractDO);
				resp.setData(contractDO);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(contractDO, e);
		}
		return resp;
	}

	@Override
	public Resp<Integer> deleteById(Integer contractId) {
		Resp<Integer> resp = new Resp<>();
		try {
			int flag = contractManager.deleteById(contractId);
			resp.setData(flag);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(contractId, e);
		}
		return resp;
	}

	@Override
	public Resp<ContractDO> queryById(Integer contractId) {
		Resp<ContractDO> model = new Resp<>();
		try {
			ContractDO contractDO = contractManager.queryById(contractId);
			model.setData(contractDO);
		} catch (Exception e) {
			model.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(contractId, e);
		}
		return model;
	}

	@Override
	public Resp<ContractDO> queryDetailById(Integer contractId, Integer type) {
		Resp<ContractDO> model = new Resp<>();
		try {
			ContractDO contractDO = contractManager.queryDetailById(contractId, type);
			model.setData(contractDO);
		} catch (Exception e) {
			model.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(contractId, e);
		}
		return model;
	}

	@Override
	public Resp<ContractDO> queryReceiById(Integer contractId) {
		Resp<ContractDO> model = new Resp<>();
		try {
			ContractDO contractDO = contractManager.queryReceiById(contractId);
			model.setData(contractDO);
		} catch (Exception e) {
			model.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(contractId, e);
		}
		return model;
	}

	@Override
	public PageResult<ContractDO> queryPage(Page page, ContractQueryVO queryVO) {
		PageResult<ContractDO> pr = new PageResult<>();
		try {
			List<ContractDO> list = contractManager.queryPage(page, queryVO);

			PageUtils.copyPage(pr, page);
			pr.setData(list);
		} catch (Exception e) {
			pr.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(new Object[] { page, queryVO }, e);
		}
		return pr;
	}

	@Override
	public Resp<Integer> updateContract(ContractDO contractDO, BindingResult errors) {
		Resp<Integer> resp = new Resp<>();
		try {
			super.validate(resp, errors, contractDO);
			if (resp.isSuccess()) {
				int flag = contractManager.updateContract(contractDO);
				resp.setData(flag);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(contractDO, e);
		}
		return resp;
	}

	@Override
	public Resp<List<ContractDO>> queryList() {
		Resp<List<ContractDO>> resp = new Resp<>();
		try {
			List<ContractDO> list = contractManager.queryList();
			resp.setData(list);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			super.writeLog(e);
		}
		return resp;
	}

}
